package com.terabits.smartriver.netty.config;

import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelPipeline;
import io.netty.channel.socket.SocketChannel;
import io.netty.handler.codec.DelimiterBasedFrameDecoder;
import io.netty.handler.codec.string.StringDecoder;
import io.netty.handler.logging.LogLevel;
import io.netty.handler.logging.LoggingHandler;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
public class ChildChannelInitializer extends ChannelInitializer<SocketChannel> {

    @Autowired
    private ServerHandler serverHandler;

    @Autowired
    private PacketDecoder packetDecoder;

    @Override
    protected void initChannel(SocketChannel socketChannel) throws Exception {
        ByteBuf delimiter= Unpooled.copiedBuffer(";#".getBytes());
        ChannelPipeline pipeline = socketChannel.pipeline();
//        pipeline.addLast(new LineBasedFrameDecoder(1024));
        pipeline.addLast(new LoggingHandler(LogLevel.DEBUG));
        pipeline.addLast(new DelimiterBasedFrameDecoder(1024*10*10,delimiter));
        pipeline.addLast(new StringDecoder());
        pipeline.addLast(serverHandler);
    }
}
